package com.guigu.pd.auth.service.impl;

import com.guigu.pd.auth.entity.CoreOrgEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.Query;

import com.guigu.pd.auth.dao.AuthMenuDao;
import com.guigu.pd.auth.entity.AuthMenuEntity;
import com.guigu.pd.auth.service.AuthMenuService;


@Service("authMenuService")
public class AuthMenuServiceImpl extends ServiceImpl<AuthMenuDao, AuthMenuEntity> implements AuthMenuService {

    @Autowired
    AuthMenuDao authMenuDao;

    @Override
    public List<AuthMenuEntity> MenusTree(Map<String, Object> params) {
        //查所有菜单
        List<AuthMenuEntity> allMenu = authMenuDao.findAllMenus();


        // 2. 组装成父子的树形结构

        List<AuthMenuEntity> leve1Menus = allMenu.stream().filter(
                authMenuEntity -> authMenuEntity.getParentId() == 0
        ).map(menu -> {
            menu.setChildren(getChildrens(menu, allMenu));
            return menu;

        }).sorted(Comparator.comparingInt(menu -> (menu.getSortValue() == null ? 0 :
                menu.getSortValue()))).collect(Collectors.toList());

        return leve1Menus;
    }

    //增加菜单
    @Override
    public int insertMenu(AuthMenuEntity authMenuEntity) {
        int num = 0;
        QueryWrapper<AuthMenuEntity> queryWrapper = new QueryWrapper<AuthMenuEntity>();
        queryWrapper.eq("name", authMenuEntity.getName());
        AuthMenuEntity one = this.getOne(queryWrapper);


        if (one == null) {
            boolean save = this.save(authMenuEntity);

            if (save) {
                num = 1;
            }
        }
        return num;
    }

    //逻辑删除菜单
    @Override
    public int delmenu(int id) {
        int delmenu = authMenuDao.delmenu(id);

        return delmenu;
    }

    //根据id查菜单
    @Override
    public AuthMenuEntity queryMenusByid(Long id) {
        return this.getById(id);
    }

    //根据id编辑菜单
    @Override
    public int updateMenu(AuthMenuEntity authMenuEntity) {
        int num = 0;
        boolean b = this.updateById(authMenuEntity);
        if (b) {
            num = 1;
        }
        return num;
    }

    /**
     * 递归查找所有菜单的子菜单
     */
    private List<AuthMenuEntity> getChildrens(AuthMenuEntity root, List<AuthMenuEntity> allMenus) {
        List<AuthMenuEntity> children =
                allMenus.stream().filter(authMenuEntity -> authMenuEntity.getParentId().equals(root.getId())).map(authMenuEntity -> {
                    authMenuEntity.setChildren(getChildrens(authMenuEntity, allMenus));
                    return authMenuEntity;
                }).sorted(Comparator.comparing(menu -> (menu.getSortValue() == null ? 0 :
                        menu.getSortValue()))).collect(Collectors.toList()
                );

        return children;
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AuthMenuEntity> page = this.page(
                new Query<AuthMenuEntity>().getPage(params),
                new QueryWrapper<AuthMenuEntity>()
        );

        return new PageUtils(page);
    }
}